﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcDDshop2.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Create()
        {
            List<type> types = BLL.Bll.select_type();
            ViewBag.types = types; 
            return View();
        }

        //添加书籍
        [HttpPost]
        public ActionResult Create(string a)
        {
            List<type> types = BLL.Bll.select_type();
            ViewBag.types = types;
            if (Request["bookname"] == "" || Request["bookname"]==null) 
            {
                Response.Write("<script>alert('不能为空！')</script>");
                return View();
            }
            if (Request["Author"] == "" || Request["Author"] == null)
            {
                Response.Write("<script>alert('不能为空！')</script>");
                return View();
            }
            if (Request["Press"] == "" || Request["Press"] == null)
            {
                Response.Write("<script>alert('不能为空！')</script>");
                return View();
            }
            if (Request["Price"] == "" || Request["Price"] == null)
            {
                Response.Write("<script>alert('不能为空！')</script>");
                return View();
            }
            if (Request["Pic"] == "" || Request["Pic"] == null)
            {
                Response.Write("<script>alert('不能为空！')</script>");
                return View();
            }
            if (Request["Type"] == "" || Request["Type"] == null)
            {
                Response.Write("<script>alert('不能为空！')</script>");
                return View();
            }
            if (Request["Introduced"] == "" || Request["Introduced"] == null)
            {
                Response.Write("<script>alert('不能为空！')</script>");
                return View();
            }
            if (Request["Show"] == "" || Request["Show"] == null)
            {
                Response.Write("<script>alert('不能为空！')</script>");
                return View();
            }
            else
            {
                Book book = new Book();
                book.bookname = Request["bookname"];
                book.Author = Request["Author"];
                book.Press = Request["Press"];
                book.Price = Convert.ToDecimal(Request["Price"]);
                book.Pic = Request["Pic"];
                book.Type = Convert.ToInt32(Request["Type"]);
                book.Introduced = Request["Introduced"];
                book.Show = Convert.ToInt32(Request["Show"]);

                var pdf = Request.Files[0];
                if (Request.Files[0].ContentLength == null)
                {
                    Response.Write("<script>alert('不能为空！')</script>");
                    return View();
                }
                else
                {
                    byte[] p = new byte[pdf.InputStream.Length];
                    pdf.InputStream.Read(p, 0, p.Length);
                    using (SqlConnection conn = new SqlConnection("Server=rm-bp13x228e5ip7ogsyzo.sqlserver.rds.aliyuncs.com,3433;uid=student;pwd=abc123!@#;database =Team4_DDBookDB"))
                    {
                        conn.Open();
                        string sql = @"insert into BookInfo values('" + book.bookname + "','" + book.Author + "','" + book.Press + "'," + book.Price + ",'" + book.Pic + "'," + book.Type + ",'" + book.Introduced + "'," + book.Show + ",@FileData)";
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        cmd.Parameters.Add("@FileData", SqlDbType.VarBinary);
                        cmd.Parameters["@FileData"].Value = p;
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                        conn.Close();
                    }
                    return View();
                }
            }
        }

        public ActionResult Home()
        {
            List<Book> books = BLL.Bll.select_over();
            ViewBag.books = books;
            return View();
        }

        public ActionResult update()
        {
            int id = Convert.ToInt32(Request["id"]);
            Book book = BLL.Bll.book_id(id);
            ViewBag.book = book;
            return View();
        }

        [HttpPost]
        public ActionResult update(int a = 0)
        {
            int id = Convert.ToInt32(Request["id"]);
            string Author = Request["Author"].ToString();
            string bookname = Request["bookname"].ToString();
            string Press = Request["Press"].ToString();
            string price = Request["price"].ToString();
            string Introduced = Request["Introduced"].ToString();
            BLL.Bll.update(id, Author, bookname, Press, price, Introduced);
            return RedirectToAction("Home");

        }

        public ActionResult dalete() 
        {
            int id = Convert.ToInt32(Request["id"]);
            BLL.Bll.delete(id,0);
            return RedirectToAction("Home");
        }

        public ActionResult insert()
        {
            int id = Convert.ToInt32(Request["id"]);
            BLL.Bll.delete(id,1);
            return RedirectToAction("Home");
        }
    }
}
